package com.vivi.codereview.actors;

import java.io.File;
import java.util.Map;
import java.util.Set;

import com.vivi.codereview.WhatTheFuckException;
import com.vivi.codereview.tools.Log;

/**
 * Is the associated reviewer of Developper (AgileDeveloperActor). This actor
 * will ask for the AgileReview project and the stream to the reviewer, then we will check
 * status of each impacted file, then try to unzip the fix, let the user doing
 * some diffs and, in the end, remove all hijacks (and destroy not in
 * sourceControl files).
 * @author vivi
 *
 */
public class AgileReviewerActor extends ReviewerActor {

  @Override
  public int process() throws Exception {
    Log.logSeparator();
    // We look for jdevcc folder
    String jdevccFolder = getRootDirectory();
    String reviewFolder = selectFolder(REVIEW_FOLDER,
        "Please select the Review project you want to use (mandatory step):");
    File zippedFile = getZippedFile(reviewFolder, ZIP_FILENAME);
    // we get the folder (stream) we want to use
    String rootStream = selectFolder(jdevccFolder, "Please select index of stream you want to use in list above (mandatory step):");
    String logFile = getZipUtil().readFileInZip(zippedFile, ZIP_LOG_FILENAME);
    Log.log(logFile, Log.I);
    Set<String> files = getFilesFromLog(rootStream, logFile);
    checkFilesStatus(rootStream, files);
    // We know all files are available now - no checkout, no hijack
    Map<String, String> mapPname = getPnames(rootStream, files);
    reviewActions(rootStream, zippedFile, mapPname);
    return Log.Exit.OK;
  }

  /**
   * Retrieve the zip file to be considered.
   *
   * @param root the review folder (not the one with the zip)
   * @param zipFilename
   * @return
   */
  @Override
  protected File getZippedFile(String root, String zipFilename) throws Exception {
    String reviewFolder = getSysUtil().getFullFilename(root, SUB_ZIP_FOLDER);
    File file = getSysUtil().getFile(reviewFolder, zipFilename);
    Log.log("Selected zip file: " + file, Log.D);
    if (!file.exists()) {
      Log.log("Zip file " + file + "doesn't exist. We cannot continue. Bye bye", Log.E);
      throw new WhatTheFuckException(Log.Exit.ZIP_FILE_NOT_FOUND);
    }
    return file;
  }

	@Override
	public String getName() {
		return "Reviewer";
	}

}
